package com.youshang520i.springconfigzookeeper;

import lombok.Data;
import lombok.ToString;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author youshang
 */

@SpringBootApplication
@Log4j2
@RestController
public class SpringZookeeperConfig {

    public static void main(String[] args) {
        SpringApplication.run(SpringZookeeperConfig.class);
    }


    @ConfigurationProperties(prefix = "config")
    @Configuration
    @Data
    @ToString
    public class ConfigProperties {
        private String env;
        private String version;
    }

    // 全局配置
    @ConfigurationProperties(prefix = "jdbc")
    @Configuration
    @Data
    @ToString
    public class JdbcConfigProperties {
        private String url;
        private String username;
        private String password;
    }

    @Autowired
    private ConfigProperties configProperties;
    @Autowired
    private JdbcConfigProperties jdbcConfigProperties;

    @GetMapping("")
    public void init(){
        log.info("config -> {}", configProperties);
        log.info("jdbc-config -> {}", jdbcConfigProperties);
    }


//    @Component
//    public class MyRunner implements CommandLineRunner {
//        @Override
//        public void run(String... args) throws Exception {
//            Executors.newSingleThreadExecutor().submit(() -> {
//                while (true) {
//                    log.info("config -> {}", configProperties);
//                    log.info("jdbc-config -> {}", jdbcConfigProperties);
//                    TimeUnit.SECONDS.sleep(5);
//                }
//            });
//        }
//    }


}
